import { ElLoading as Loading } from 'element-plus';
import _ from 'lodash';
let needLoadingRequestCount = 0;
let loading:any;

function startLoading() {
    loading = Loading.service({
        spinner: 'topLoading',
        lock: true,
        background: 'rgba(255, 255, 255, 0.0)'
    })
}

function endLoading() {
    loading.close()
}

const tryCloseLoading = () => {
    if (needLoadingRequestCount === 0) {
        endLoading();
    }
};

export function showLoading() {
    if (needLoadingRequestCount === 0) {
        startLoading();
    }
    needLoadingRequestCount++;
}

export function hideLoading() {
    if (needLoadingRequestCount <= 0) return
    needLoadingRequestCount--
    if (needLoadingRequestCount === 0) {
        _.debounce(tryCloseLoading, 300)()
    }
}